// pages/score/score.js
const memberService = require('../../api/memberService.js')
const auth = require('../../utils/auth.js')

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    scoreList: [],
    memberScoreBalance: 0,
    mobile: '',
    page: 1,
    pageSize: 20,
    pageCount: 1,
    loading: false,
    loadingText: '正在加载...'
  },
  // 检查当前用户是否登录授权
  checkAuth: function () {
    try {
      if (!app.globalData.userInfo) {
        return false;
      } else {
        return true;
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  // 获取积分余额
  getScoreBalance: function () {
    memberService.getBalance(this.data.mobile).then(res => {
      console.log(res)
      this.setData({
        memberScoreBalance: res
      })
    }).catch(e => {
      wx.showToast({
        title: e.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 获取列表
  getMemberScoreBalance: function (page, pageSize) {
    const loginStatus = this.checkAuth()
    if (loginStatus) {
      this.queryScoreData(page, pageSize)
    } else {
      wx.showToast({
        title: '请登录后查询积分明细',
        icon: 'none',
        duration: 2000
      })
    }
  },
  // 查询积分数据
  queryScoreData: function (page, pageSize) {
    const that = this
    const mobile = this.data.mobile
    let list = memberService.queryHst(page, pageSize, mobile).then(res => {
      console.log(res)
      let tempScoreList = this.data.scoreList;
      if (res.records && res.records.length >= 0) {
        res.records.forEach(item => {
          let tempItem = {}
          if (item.occur > 0) {
            let occur = '+' + item.occur
            tempItem = {
              ...item,
              occur: occur,
              increase: true
            }
          } else if (item.occur < 0) {
            let occur = 0 - item.occur
            occur = '-' + occur
            tempItem = {
              ...item,
              occur: occur,
              increase: false
            }
          } else {
            tempItem = {
              ...item,
              occur: '-0.00',
              increase: false
            }
          }
          tempItem = {
            ...tempItem,
            action: item.remark,
            // action: that.handleAction(item.action)
          }
          tempScoreList.push(tempItem)
        });
        let pageCount = parseInt(res.recordCount / res.pageSize) + 1;
        this.setData({
          page: res.page,
          pageCount: pageCount,
          scoreList: tempScoreList
        })
        return tempScoreList
      }
    }).catch(error => {
      wx.showToast({
        title: error.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 操作记录
  handleAction: function (value) {
    if (value === 'recharge') {
      return '充值'
    } else if (value === 'consume') {
      return '消费'
    } else if (value === 'adjust') {
      return '调整'
    } else if (value === 'checkIn') {
      return '签到'
    } else if (value === 'refund') {
      return '退款'
    } else {
      return value
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    try {
      var _userInfo = auth.getUser();
      if (_userInfo && _userInfo.member) {
        let mobile = _userInfo.member.mobile
        that.setData({
          mobile: mobile
        })
        that.getScoreBalance()
        that.getMemberScoreBalance(this.data.page, this.data.pageSize)
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      pageCount: 1,
      scoreList: []
    })
    this.getMemberScoreBalance(this.data.page, this.data.pageSize)
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page < this.data.pageCount) {
      let index = this.data.page;
      ++index;
      this.queryScoreData(index, this.data.pageSize)
    } else {
      wx.showToast({
        title: '没有更多数据~',
        icon: 'none',
        duration: 1500
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})